package org.example.multiThreading.threadPool;

import java.util.concurrent.*;

public class TestThreadPoolExecutor {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 8, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5));
        //创建TestCallable，并加入线程池
        for (int i = 0; i < 10; i++) {
            TestCallable callable = new TestCallable();
            System.out.println(executor.submit(callable).get());
        }

        //删除线程池,执行完全部任务后停止
        executor.shutdown();
        //删除线程池，立即停止
        executor.shutdownNow();
    }

}

class TestCallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        int u = 0;
        for (int i = 0; i < 100; i++) {
            u+=i;
        }
        return u;
    }
}
